package cn.com.springboot.controller;

import cn.com.springboot.entity.Client;
import cn.com.springboot.service.ClientService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class userController {

    @Resource
    private ClientService clientService;

    @RequestMapping("/show")
    public String show(){
        return "register";
    }

    @RequestMapping("/show3")
    public String show3(){
        return "login";
    }


    @RequestMapping("/login")
    public String login(@ModelAttribute Client client, Model model, HttpSession session){
        if (clientService.login(client)!=null){
            session.setAttribute("list",clientService.selectAll());
            return "homepage";
        }else{
            return "login";
        }
    }


    @RequestMapping("/register")
    public String register(@ModelAttribute Client client){
        if (clientService.add(client)>0){
            return "login";
        }else {
            return "register";
        }
    }

    @RequestMapping("/delete")
    public String delete(int cid,HttpSession session){
        clientService.delete(cid);
        session.setAttribute("list",clientService.selectAll());
        return "homepage";
    }

    @RequestMapping("/updateJSP")
    public String updateJSP(int cid, HttpSession session){
        session.setAttribute("list",clientService.query(cid));
        return "update";
    }

    @RequestMapping("/update")
    public String update(@ModelAttribute Client client, HttpSession session){
        clientService.updateClient(client);
        session.setAttribute("list",clientService.selectAll());
        return "homepage";
    }
}
